import request from '@/utils/request'
import type { 
  ApiResponse, 
  Post, 
  PostRequest, 
  PostQueryParams, 
  PostStats,
  PageResult 
} from '@/types'

// 文章相关API
export const postApi = {
  // 创建文章
  createPost: (data: PostRequest): Promise<ApiResponse<Post>> => {
    return request.post('/posts', data)
  },

  // 更新文章
  updatePost: (id: number, data: Partial<PostRequest>): Promise<ApiResponse<Post>> => {
    return request.put(`/posts/${id}`, data)
  },

  // 获取文章详情
  getPostDetail: (id: number): Promise<ApiResponse<Post>> => {
    return request.get(`/posts/${id}`)
  },

  // 删除文章
  deletePost: (id: number): Promise<ApiResponse<string>> => {
    return request.delete(`/posts/${id}`)
  },

  // 查询文章列表
  getPostList: (params: PostQueryParams): Promise<ApiResponse<PageResult<Post>>> => {
    // 转换参数名称以匹配后端API
    const transformedParams = {
      ...params,
      pageNo: params.pageNo,
      pageSize: params.pageSize
    }
    return request.get('/posts', { params: transformedParams })
  },

  // 获取文章统计数据
  getPostStats: (id: number): Promise<ApiResponse<PostStats>> => {
    return request.get(`/posts/${id}/stats`)
  },

  // 增加文章浏览量
  incrementViewCount: (id: number): Promise<ApiResponse<string>> => {
    return request.put(`/posts/${id}/view`)
  }
}